/*
 * @Author: windys
 * @Description: 
 * @Date: 2022-02-16 11:20:36
 */
// store/index.js
import { createStore } from 'vuex'
import { ElMessage } from 'element-plus'
import { getCarNumber } from '@/api/car'
import { getSysColor } from '@/api/mall'
import storage from "@/utils/storage";

export default createStore({
    state: {
        // / 获取缓存中的显示文字类型，如果为空默认为中文
        language: window.localStorage.getItem('LANG') || "zh",
        themeColor: '#A0C460',
        carNums: 0
    },
    getters: {
        language(state) {
            return state.language
        },
        themeColor(state) {
            return state.themeColor
        }
    },
    mutations: {
        setLanguage(state, lang) {
            window.localStorage.setItem('LANG', lang);
            state.language = lang;
            ElMessage({
                type: 'success',
                message: 'success！'
            })
            console.log(state.language, '语言已切换')
        },

    },
    actions: {
        setTheme: async (state, color) => {
            const res = await getSysColor({})

            if (res) {
                const el = document.documentElement
                el.style.setProperty('--el-color-primary', res ? res.gcolor : '#A0C460')
                el.style.setProperty('--el-color-success', res ? res.gcolor : '#A0C460')
                state.state.themeColor = res ? res.gcolor : '#A0C460'
            }
        },
        getCarNums: async (state) => {
            const res = await getCarNumber({})
            console.log(res, '购物车数量')
            if (typeof res == 'object') {
                state.state.carNums = res.data
            } else {
                state.state.carNums = res
            }
        }
    },
})
